package com.xjscrm.console.chat.core.impl;

import com.xjscrm.common.entity.WwDevice;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.frame.dto.ChatDeviceDTO;
import com.xjscrm.console.chat.frame.recv.RPullTalkDeviceMsg;
import com.xjscrm.console.chat.frame.send.SPullDeviceResultMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import com.xjscrm.console.entity.ChatTalkSessionStaffRel;
import com.xjscrm.console.entity.MerchatChildDeviceRelation;
import com.xjscrm.console.param.wwDevice.WwDeviceQuery;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionStaffRelService;
import com.xjscrm.console.service.customer.merchat.MerchatChildDeviceRelationService;
import com.xjscrm.console.service.customer.wwDevice.WwDeviceService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author mingyang.chen create in 2024/3/12 09:41
 */
@Component
@Slf4j
public class ChatPullTalkDeviceMsgHandler extends ChatBaseMsgHandler<RPullTalkDeviceMsg> {

    @Autowired
    private WwDeviceService deviceService;

    @Autowired
    private ChatTalkSessionStaffRelService chatTalkSessionStaffRelService;

    @Autowired
    private MerchatChildDeviceRelationService merchatChildDeviceRelationService;


    @Override
    public SResultMsg doHandle(RPullTalkDeviceMsg content, ChannelHandlerContext ctx) {

        SPullDeviceResultMsg pullDeviceResultMsg = new SPullDeviceResultMsg();
        try {
            List<Long> deviceUniqueIds;

            Boolean onlyTalk = content.getOnlyTalk();
            if (onlyTalk) {
                List<ChatTalkSessionStaffRel> sessionStaffRelList = chatTalkSessionStaffRelService.listByStaffId(content.getMerId(), content.getStaffId());
                if (ObjectUtils.isEmpty(sessionStaffRelList)) {
                    return pullDeviceResultMsg;
                }
                deviceUniqueIds = sessionStaffRelList.stream().map(ChatTalkSessionStaffRel::getDeviceUniqueId).distinct().collect(Collectors.toList());
            } else {
                List<MerchatChildDeviceRelation> merchatChildDeviceRelations = merchatChildDeviceRelationService.listByStaffId(content.getMerId(), content.getStaffId());
                if (ObjectUtils.isEmpty(merchatChildDeviceRelations)) {
                    return pullDeviceResultMsg;
                }
                deviceUniqueIds = merchatChildDeviceRelations.stream().map(MerchatChildDeviceRelation::getRobotId).distinct().collect(Collectors.toList());
            }

            WwDeviceQuery wwDeviceQuery = new WwDeviceQuery();
            wwDeviceQuery.setMerId(content.getMerId());
            wwDeviceQuery.setDeviceUniqueIds(deviceUniqueIds);

            List<WwDevice> devices = deviceService.listByCondition(wwDeviceQuery);

            if (ObjectUtils.isEmpty(devices)) {
                return pullDeviceResultMsg;
            }
            List<ChatDeviceDTO> deviceMsgs = new ArrayList<>();
            for (WwDevice device : devices) {
                ChatDeviceDTO sDeviceMsg = new ChatDeviceDTO();
                BeanUtils.copyProperties(device, sDeviceMsg);

                deviceMsgs.add(sDeviceMsg);
            }
            pullDeviceResultMsg.setDevices(deviceMsgs);
            pullDeviceResultMsg.setResultType(ChatMsgType.s_pull_device.getCode());
            pullDeviceResultMsg.setResultTypeDesc(ChatMsgType.s_pull_device.getDesc());
        } catch (Exception e) {
            log.error("拉取员工机器人列表失败", e);
            throw new BusinessException("拉取员工机器人列表失败");
        }
        return pullDeviceResultMsg;
    }

    @Override
    public boolean checkMsg(RPullTalkDeviceMsg baseMsg) {
        return super.checkMsg(baseMsg) || !Objects.nonNull(baseMsg.getStaffId());
    }

    @Override
    public Class<RPullTalkDeviceMsg> msgClass() {
        return RPullTalkDeviceMsg.class;
    }

    @Override
    public ChatMsgType msgType() {
        return ChatMsgType.r_pull_device;
    }
}
